#pragma once

#include "qt_graphics_shape_point.h"
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsItem>
#include <QPainter>
#include <QPen>
class GraphicsShapeRect : public QObject,public QAbstractGraphicsShapeItem
{
    Q_OBJECT
public:

    BPointItem *m_point1;
    BPointItem *m_point2;

    QPen m_pen;
    GraphicsShapeRect::GraphicsShapeRect(qreal x, qreal y, qreal width, qreal height,QColor color)
    {
        //true hoverEnterEvent hoverLeaveEvent start
        this->setAcceptHoverEvents(true);

        this->setFlags(QGraphicsItem::ItemIsSelectable |
                       QGraphicsItem::ItemIsMovable |
                       QGraphicsItem::ItemIsFocusable);
        this->setCursor(Qt::PointingHandCursor);
        m_point1 = new BPointItem(this, QPointF(x,y));

        m_point2 = new BPointItem(this, QPointF(x+width,y+height));
        m_pen.setColor(color);
        m_pen.setStyle(Qt::SolidLine);

    }

    QRectF GraphicsShapeRect::boundingRect() const
    {
        qreal x_min,x_max,y_min,y_max;
        if(m_point1->pos().x() <m_point2->pos().x())
        {
            x_min = m_point1->pos().x();
            x_max = m_point2->pos().x();
        }
        else
        {
            x_min = m_point2->pos().x();
            x_max = m_point1->pos().x();
        }

        if(m_point1->pos().y() <m_point2->pos().y())
        {
            y_min = m_point1->pos().y();
            y_max = m_point2->pos().y();
        }
        else
        {
            y_min = m_point2->pos().y();
            y_max = m_point1->pos().y();
        }
        return QRectF(QPointF(x_min,y_min),QPointF(x_max,y_max));
    }

    virtual void GraphicsShapeRect::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {
        Q_UNUSED(option);
        Q_UNUSED(widget);

        painter->setPen(m_pen);
        m_pen.setWidth(9);
        painter->drawRect(boundingRect());
    }

    virtual void GraphicsShapeRect::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
    {
        m_pen.setColor(Qt::green);
        m_pen.setStyle(Qt::SolidLine);
        QGraphicsItem::hoverEnterEvent(event);
    }

    virtual void GraphicsShapeRect::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
    {
        m_pen.setColor(Qt::blue);
        m_pen.setStyle(Qt::SolidLine);
        QGraphicsItem::hoverLeaveEvent(event);
    }

    virtual void GraphicsShapeRect::hoverMoveEvent(QGraphicsSceneHoverEvent* event)
    {
        QPointF pos = event->pos();
        emit sendMessage(pos);
    }

signals:
    void sendMessage(QPointF pos);
};

